﻿
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using WindowsInput;
using static AutoTestSystem.ToolClass;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace AutoTestSystem
{
    public partial class StepForm : Form
    {

        List<FunctionClass> funcList = new List<FunctionClass>();

        private Form1 _form1;
        private static string textBoxValue;
        int mode = 0;
        List<Form> formList = new List<Form>();
        Edit setForm;
        private MouseHookLib _keyboardHook = null;
        string stepId;
        bool modeBool;
        public Func<string> ContentGetter { get; set; }
        public StepForm(Edit sf,bool modeB,string id)
        {
            InitializeComponent();
            
            CheckForIllegalCrossThreadCalls = false;
            this.MaximizeBox = false;
            setForm = sf;
            stepId = id;
            modeBool = modeB;

            //uiButton1.Click += uiButton1_Click;
            uiButton2.Click += uiButton2_Click;
            uiButton3.Click += uiButton3_Click;
            uiButton4.Click += uiButton4_Click;
            uiButton5.Click += uiButton5_Click;
            uiButton6.Click += uiButton6_Click;
            uiButton7.Click += uiButton7_Click;
            uiButton8.Click += uiButton8_Click;

            //if (modeB)
            //{
            //    uiTextBox1.Text = string.Empty;
            //    uiTextBox2.Text = string.Empty;
            //    uiTextBox3.Text = string.Empty;
            //    uiTextBox4.Text = string.Empty;
            //    uiTextBox5.Text = string.Empty; 

            //}
            //else
            //{
               
            //    uiTextBox1.ReadOnly = true;
            //    foreach (StepClass item in Form1.stepList)
            //    {
            //        if (item.StepName == id)
            //        {
            //            uiTextBox1.Text = item.StepName;
            //            uiTextBox2.Text = item.StepDetail;
            //            uiTextBox4.Text= item.CompareStr;
            //            uiTextBox5.Text = item.Area;
            //            break;
            //        }
            //    }
            //}
            

            //uiTextBox3.Text = string.Empty;
            Thread th = new Thread(new ThreadStart(ThreadMethod));
            th.IsBackground = true;
            th.Start();

        }
        //public string GetTextBoxContent()
        //{
        //    return uiTextBox1.Text;
        //}
        public static string TextBoxValue
        {
            get { return textBoxValue; }
            set { textBoxValue = value; }
        }

        //private void uiButton1_Click(object sender, EventArgs e)
        //{
        //    if (mode == 1 || mode == 11)
        //    {
        //        MessageBox.Show("请点击选择坐标");
        //        return;
        //    }

        //    if (uiTextBox1.TextLength == 0 || uiTextBox1.Text == string.Empty)
        //    {
        //        MessageBox.Show("请输入机种名");
        //        return;
        //    }


        //    if (modeBool)
        //    {
        //        int num = 0;

        //        for (int i = 0; i < Form1.stepList.Count; i++)
        //        {
        //            if (Form1.stepList[i].StepName == uiTextBox1.Text)
        //            {
        //                num++;
        //                break;
        //            }
        //        }

        //        if (num == 0)
        //        {
        //            StepClass stepClass = new StepClass();
        //            stepClass.StepName = uiTextBox1.Text;
        //            stepClass.StepDetail = uiTextBox2.Text;
        //            stepClass.CompareStr = uiTextBox4.Text;
        //            stepClass.MonitorArea = "查询";
        //            stepClass.IsEnable = "✘";
        //            stepClass.Area = uiTextBox5.Text;

        //            Form1.stepList.Add(stepClass);

        //            ToolClass.AddLocData(stepClass);


        //            MessageBox.Show("保存成功");


        //            setForm.uiDataGridView1.DataSource = null;
        //            setForm.uiDataGridView1.DataSource = Form1.stepList;

        //            Form1.AddStepData = true;

        //            setForm.uiDataGridView1.Columns[0].FillWeight = 42;
        //            setForm.uiDataGridView1.Columns[1].FillWeight = 42;
        //            setForm.uiDataGridView1.Columns[2].FillWeight = 42;
        //            setForm.uiDataGridView1.Columns[3].FillWeight = 140;
        //            setForm.uiDataGridView1.Columns[4].FillWeight = 220;
        //            setForm.uiDataGridView1.Columns[5].FillWeight = 110;
        //            setForm.uiDataGridView1.Columns[6].FillWeight = 50;
        //            setForm.uiDataGridView1.Columns[7].FillWeight = 35;
        //            setForm.uiDataGridView1.Columns[6].DefaultCellStyle.ForeColor = Color.Blue;

        //            for (int i = 0; i < setForm.uiDataGridView1.Rows.Count; i++)
        //            {
        //                if (setForm.uiDataGridView1.Rows[i].Cells[7].Value.ToString() == "✔")
        //                {
        //                    setForm.uiDataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Green;
        //                    setForm.uiDataGridView1.Rows[i].DefaultCellStyle.ForeColor = Color.White;
        //                    setForm.uiDataGridView1.Rows[i].Cells[6].Style.ForeColor = Color.Blue;
        //                }
        //                else
        //                {
        //                    setForm.uiDataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.White;
        //                    setForm.uiDataGridView1.Rows[i].DefaultCellStyle.ForeColor = Color.Black;
        //                    setForm.uiDataGridView1.Rows[i].Cells[6].Style.ForeColor = Color.Blue;
        //                }
        //            }
        //        }
        //        else
        //        {
        //            MessageBox.Show("机种名重复");
        //            return;
        //        }

               
        //    }
        //    else
        //    {
        //        foreach (StepClass item in Form1.stepList)
        //        {
        //            if (item.StepName == stepId)
        //            {
        //                item.StepDetail = uiTextBox2.Text;
        //                item.StepName = uiTextBox1.Text;
        //                item.CompareStr = uiTextBox4.Text;
        //                item.Area = uiTextBox5.Text;
        //                ToolClass.UpdateLocData(item);

        //                break;
        //            }
        //        }




        //        MessageBox.Show("更新成功");


        //        setForm.uiDataGridView1.DataSource = null;
        //        setForm.uiDataGridView1.DataSource = Form1.stepList;

        //        Form1.AddStepData = true;

        //        setForm.uiDataGridView1.Columns[0].FillWeight = 42;
        //        setForm.uiDataGridView1.Columns[1].FillWeight = 42;
        //        setForm.uiDataGridView1.Columns[2].FillWeight = 42;
        //        setForm.uiDataGridView1.Columns[3].FillWeight = 140;
        //        setForm.uiDataGridView1.Columns[4].FillWeight = 220;
        //        setForm.uiDataGridView1.Columns[5].FillWeight = 110;
        //        setForm.uiDataGridView1.Columns[6].FillWeight = 50;
        //        setForm.uiDataGridView1.Columns[7].FillWeight = 35;
        //        setForm.uiDataGridView1.Columns[6].DefaultCellStyle.ForeColor = Color.Blue;
        //        for (int i = 0; i < setForm.uiDataGridView1.Rows.Count; i++)
        //        {
        //            if (setForm.uiDataGridView1.Rows[i].Cells[7].Value.ToString() == "✔")
        //            {
        //                setForm.uiDataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Green;
        //                setForm.uiDataGridView1.Rows[i].DefaultCellStyle.ForeColor = Color.White;
        //                setForm.uiDataGridView1.Rows[i].Cells[6].Style.ForeColor = Color.Blue;
        //            }
        //            else
        //            {
        //                setForm.uiDataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.White;
        //                setForm.uiDataGridView1.Rows[i].DefaultCellStyle.ForeColor = Color.Black;
        //                setForm.uiDataGridView1.Rows[i].Cells[6].Style.ForeColor = Color.Blue;
        //            }
        //        }
        //    }


        //    this.Close();
        //}



        public void MouseDownHook(object sender, MouseEventArgs e)
        {
          
            
            if (mode == 11)
            
            {
                mode = 0;

                FunctionClass fc=new FunctionClass();
                fc.FunctionId = (funcList.Count+1).ToString();
                fc.FunctionName = "移动位置";
                fc.FunctionDetail = Cursor.Position.X + "," + Cursor.Position.Y;
                funcList.Add(fc);   

                //uiTextBox2.Text += Cursor.Position.X + "," + Cursor.Position.Y + "-";
                foreach (Form form in formList) // 获取当前应用程序打开的所有窗体
                {
                    form.WindowState = FormWindowState.Normal;
                }
                formList.Clear();
            }



            if (_keyboardHook != null) _keyboardHook.UninstallHook();


            uiDataGridView1.DataSource = null;
            uiDataGridView1.DataSource = funcList;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //if(uiTextBox2.Text)
        }


        void ThreadMethod()
        {
            while (true)
            {
                if (mode == 1)
                {
                    foreach (Form form in Application.OpenForms) // 获取当前应用程序打开的所有窗体
                    {
                        form.WindowState = FormWindowState.Minimized;
                        formList.Add(form);
                    }
                    mode = 11;
                }
                else if (mode == 2)
                {
                    foreach (Form form in Application.OpenForms) // 获取当前应用程序打开的所有窗体
                    {
                        form.WindowState = FormWindowState.Minimized;
                        formList.Add(form);
                    }
                    mode = 12;
                }
            }


        }
        private void uiButton2_Click(object sender, EventArgs e)
        {
            mode = 1;
           
            //_keyboardHook.MouseUp += new MouseEventHandler(MouseUp);
            _keyboardHook = new MouseHookLib();
            _keyboardHook.MouseDown += new MouseEventHandler(MouseDownHook);
            _keyboardHook.InstallHook();
        }

        private void uiButton3_Click(object sender, EventArgs e)
        {
            if (mode == 1 || mode == 11)
            {
                MessageBox.Show("請點擊選擇坐標");
                return;
            }

          
                FunctionClass fc = new FunctionClass();
                fc.FunctionId = (funcList.Count+1).ToString();
                fc.FunctionName = "左键单击";
                fc.FunctionDetail = "-";

                funcList.Add(fc);


            uiDataGridView1.DataSource = null;
            uiDataGridView1.DataSource = funcList;

            //uiTextBox2.Text += "1-";
        }

        private void uiButton4_Click(object sender, EventArgs e)
        {
            if (mode == 1 || mode == 11)
            {
                MessageBox.Show("請點擊選擇坐標");
                return;
            }

           
                FunctionClass fc = new FunctionClass();
                fc.FunctionId = (funcList.Count + 1).ToString();
                fc.FunctionName = "右键单击";
                fc.FunctionDetail = "-";

                funcList.Add(fc);
            uiDataGridView1.DataSource = null;
            uiDataGridView1.DataSource = funcList;
        }

        private void uiButton5_Click(object sender, EventArgs e)
        {
            if (mode == 1 || mode == 11)
            {
                MessageBox.Show("請點擊選擇坐標");
                return;
            }

          
                FunctionClass fc = new FunctionClass();
                fc.FunctionId = (funcList.Count + 1).ToString();
                fc.FunctionName = "左键双击";
                fc.FunctionDetail = "-";

                funcList.Add(fc);
            uiDataGridView1.DataSource = null;
            uiDataGridView1.DataSource = funcList;
        }

        private void uiButton6_Click(object sender, EventArgs e)
        {
            if (mode == 1 || mode == 11)
            {
                MessageBox.Show("請點擊選擇坐標");
                return;
            }

         
                FunctionClass fc = new FunctionClass();
                fc.FunctionId = (funcList.Count + 1).ToString();
                fc.FunctionName = "右键双击";
                fc.FunctionDetail = "-";

                funcList.Add(fc);
            uiDataGridView1.DataSource = null;
            uiDataGridView1.DataSource = funcList;
        }

        private void uiButton7_Click(object sender, EventArgs e)
        {
            if (mode == 1 || mode == 11)
            {
                MessageBox.Show("請點擊選擇坐標");
                return;
            }


            //if (uiTextBox3.TextLength == 0)
            //{
            //    MessageBox.Show("請填写延迟时间");
            //    return;
            //}

            //if (funcList.Count > 0)
            //{
            //    FunctionClass fc = new FunctionClass();
            //    fc.FunctionId = (funcList.Count + 1).ToString();
            //    fc.FunctionName = "右键双击";
            //    fc.FunctionDetail = "-";

            //    funcList.Add(fc);
            //}
            //else
            //{
            //    FunctionClass fc = new FunctionClass();
            //    fc.FunctionId = "1";
            //    fc.FunctionName = "右键双击";
            //    fc.FunctionDetail = "-";

            //    funcList.Add(fc);
            //}
            //uiTextBox2.Text += "S" + int.Parse(uiTextBox3.Text) * 1000 + "-";
        }

        private void StepForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (_keyboardHook != null) _keyboardHook.UninstallHook();
        }

        private void uiButton8_Click(object sender, EventArgs e)
        {
            //if (uiTextBox1.TextLength == 0 || uiTextBox1.Text == string.Empty)
            //{
            //    MessageBox.Show("请输入机种名");
            //    return;
            //}
            //for (int i = 0; i < setForm.uiDataGridView1.Rows.Count; i++)
            //{
            //    if (uiTextBox1.Text == setForm.uiDataGridView1.Rows[i].Cells[3].Value.ToString())
            //    {
            //        MessageBox.Show("机种名重复");
            //        return;
            //    }
            //}


            //mode = 2;

            //Delay(500);
            //ScreenCapturerTool screenCapturer = new ScreenCapturerTool(uiTextBox1.Text);

            //if (screenCapturer.ShowDialog() == DialogResult.OK)
            //{ 
            //}


            //mode = 0;
            //foreach (Form form in formList) // 获取当前应用程序打开的所有窗体
            //{
            //    form.WindowState = FormWindowState.Normal;
            //}
            //formList.Clear();
            //double widthTemp1 = Math.Abs(ScreenCapturerTool.startX - ScreenCapturerTool.finallyX) * Form1.widthRatio;
            //double heightTemp1 = Math.Abs(ScreenCapturerTool.startY - ScreenCapturerTool.finallyY) * Form1.heightRatio;
            //double X1 = ScreenCapturerTool.startX * Form1.widthRatio;
            //double Y1 = ScreenCapturerTool.startY * Form1.heightRatio;
            ////length = (int)lenth1;
            ////width = (int)width1;
            //int widthTemp = (int)widthTemp1;
            //int heightTemp = (int)heightTemp1;
            //int X = (int)X1;
            //int Y = (int)Y1;


            //uiTextBox5.Text = ScreenCapturerTool.startX + "," + ScreenCapturerTool.startY + "," + Math.Abs(ScreenCapturerTool.startX - ScreenCapturerTool.finallyX) + "," + Math.Abs(ScreenCapturerTool.startY - ScreenCapturerTool.finallyY);

            //uiTextBox5.Text = X + "," + Y+ "," + widthTemp + "," + heightTemp;
            //Form3ParametersCaptured?.Invoke(this, new Form3ParametersEventArgs(ScreenCapturerTool.startX, ScreenCapturerTool.startY, ScreenCapturerTool.finallyX, ScreenCapturerTool.finallyY));
        }

    }



   
}
